package com.wn.course

import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.blankj.utilcode.util.ToastUtils
import com.wn.common.base.BaseViewModel
import com.wn.common.model.SingleLiveData
import com.wn.course.databinding.FragmentCourseBinding
import com.wn.course.repository.ICourseRepository
import com.wn.course.repository.data.Daily
import com.wn.course.utils.UseConfigPopFilter
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2023/8/2 16:37
 * @Description: 课程中心  VM
 */
@HiltViewModel
class CourseViewModel @Inject constructor(
  private val repository: ICourseRepository,
  private val useConfigPopFilter: UseConfigPopFilter,
) :
  BaseViewModel() {
  // 四个弹窗 点击上方tabItem之后记得重新赋值
  private var type = "全部类型"     // 方向从课程分类接口获取    默认 all;
  private var level = -1     // 难度 (-1 全部) (1 初级) (2 中级) (3 高级) (4 架构) 默认 -1
  private var price = -1     // 价格 (-1, 全部) （0 付费） (1 免费) 默认 -1
  private var sort = -1   // 排序  (-1 最新) (1 评价最高)  (2 学习最多) 默认 -1
  var mainType = "全部" // 例如 android,python

  // liveData 控制 text end 图标翻转
  val typePopupOb: LiveData<Boolean> by lazy { useConfigPopFilter.typePopupOb }

  /*   val levelPopupOb: LiveData<Boolean> by lazy { useConfigPopFilter.levelPopupOb }
     val pricePopupOb: LiveData<Boolean> by lazy { useConfigPopFilter.pricePopupOb }
     val sortPopupOb: LiveData<Boolean> by lazy { useConfigPopFilter.sortPopupOb }*/
  // 点击提供 name

  val typeName = "type"
  val levelName = "level"
  val priceName = "price"
  val sortName = "sort"

  /**
   * 点击 打开 popup 弹窗
   * @param name String
   * @param v View
   */
  fun openPopup(name: String, v: View) {
    useConfigPopFilter.openPopup(name, v)
  }

  /**
   * 初始化配置  popup
   * @param mBinding FragmentCourseBinding
   */
  fun configPopFilter(mBinding: FragmentCourseBinding) =
    useConfigPopFilter.configPopFilter(mBinding, changePopValue, refreshData)

  // TabLayout 自定义 响应式数据
  val liveCourseType = repository.liveCourseType

  // TabLayout 获取自定义数据
  fun getCourseCategory() {
    apiLaunch { repository.getCourseCategory() }
  }

  // 默认值是 null 不传递 时  赋值之前的值
  private val changePopValue =
    { type: String?, level: Int?, price: Int?, sort: Int? ->
      this.type = type ?: this.type
      this.level = level ?: this.level
      this.price = price ?: this.price
      this.sort = sort ?: this.sort
    }

  // 获取数据
  fun fetchData(): Flow<PagingData<Daily.Item>> {
    val typeTitle = when (type) {
      "all" -> "全部类型"
      "business" -> "商业实战"
      else -> "专业好课"
    }
    val levelTitle = when (level) {
      -1 -> "全部难度"
      1 -> "初级"
      2 -> "中级"
      3 -> "高级"
      else -> "架构"
    }

    val priceTitle = when (price) {
      -1 -> "全部价格"
      1 -> "免费"
      else -> "付费"
    }
    val sortTitle = when (sort) {
      -1 -> "默认排序"
      0 -> "评价最高"
      else -> "学习最多"
    }
    ToastUtils.showLong("请求参数为:  $mainType $typeTitle $levelTitle $priceTitle $sortTitle")
    return repository.getDaily().cachedIn(viewModelScope)
  }

  /**
   * 重新请求数据
   * 刷新 函数  借助 liveData 驱动 Activity 中 refreshDataAtActivity
   */
  private val _refreshLive = SingleLiveData<Boolean>()
  val refreshLive: LiveData<Boolean> = _refreshLive
  private val refreshData = {
    _refreshLive.value = true
  }
}